package com.twl.ws.web;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import com.twl.app.base.ctrl.ActionSalarySum;
import com.twl.app.base.ctrl.AdjuestCtrlImpl;
import com.twl.app.base.ctrl.BfCtrlImpl;
import com.twl.app.base.ctrl.CommonCtrlImpl;
import com.twl.app.base.ctrl.DutyseviceImp;
import com.twl.app.base.ctrl.PayCoreTransLogCtrlImpl;
import com.twl.app.base.ctrl.SalaryImportImp;
import com.twl.app.base.entity.Checktemplet;
import com.twl.app.base.entity.Companyb;
import com.twl.app.base.entity.LockState;
import com.twl.app.base.entity.Uemp;
import com.twl.app.base.util.DateComm;
import com.twl.facade.web.ImportServer;
/**
 * 执行跑批
 * @author uatzhangxc
 *
 */
@Service("importServerImpl")
public class ImportServerImpl implements ImportServer {
	private Logger logger = Logger.getLogger(ImportServerImpl.class);

	@Autowired
	private CommonCtrlImpl commonCtrlImpl ;
	@Autowired 
	private BfCtrlImpl bfCtrlImpl ;
	@Autowired
	private AdjuestCtrlImpl adjuestCtrlImpl ;
	@Autowired
	private DutyseviceImp dutyseviceImp;
	@Autowired
	private SalaryImportImp salaryImportImp;
	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;
	
	//private static boolean flag = true ;
	
	@Override
	public String importChecktemplet(String period) {
		/*ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 10, 0,    //自定义线程池
				TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>());*/
		try{ 
			//查看lock锁
			LockState lockState = commonCtrlImpl.queryLockState(period);
			if(lockState!=null && "1".equals(lockState.getLockCheck())){
				return "is locked" ;
			}
			//计算		
			List<Checktemplet> checktempletList = commonCtrlImpl.getChecktempletList(period);
			if(checktempletList==null){
				return "is no checking " ;
			}
			//得到uemp的map集合
			Map<String, Uemp> map = commonCtrlImpl.listToMapForUemp();
			//线程计数
			int count = checktempletList.size();
			CountDownLatch countDownLatch = new CountDownLatch(count);
			
			logger.debug("计算开始时间:"+System.currentTimeMillis());
			System.err.println("计算开始时间:"+System.currentTimeMillis());
			
			//放入线程池
			for (int i = 0; i < count; i++) {
				
				Uemp uemp = map.get(checktempletList.get(i).getStaffint());
				if(uemp==null){
					continue ;
				}
				
				ActionSalarySum callable = new ActionSalarySum(countDownLatch,checktempletList.get(i),uemp);
				threadPoolTaskExecutor.submit(callable);
			}
			countDownLatch.await();
			
		}catch (Exception e) {
			e.printStackTrace();
			logger.debug("-----计算执行错误"+System.currentTimeMillis());
			//continue ;
		}
		System.err.println("计算结束时间:"+System.currentTimeMillis());
		logger.debug("计算结束时间:"+System.currentTimeMillis());
		
		return "计算成功！";
	}
   
	
	
	
	@Override
	public String importUemp(List<Uemp> uempList) {
	 		for (Uemp uemp : uempList) {
			//1.查询员工信息，2.进行判断
			salaryImportImp.saveump(uemp);
			commonCtrlImpl.insertJB(uemp);
		}

		return "ok";
	}

	
	
	
	
	@Override
	public String importCompanyb(List<Companyb> companybList) {
		
		for (Companyb companyb : companybList) {
			 System.out.println("--------------------------------xunhuan");
			salaryImportImp.savecompanyb(companyb);
		}
		
		return "ok";
	} 
	
	
	
	public  String action(Checktemplet checktemplet,Uemp uemp){
		
		try {
			bfCtrlImpl.insertBf(checktemplet,uemp);
			bfCtrlImpl.insertJsbf(checktemplet,uemp); //晋升补发
			adjuestCtrlImpl.insertMoneyadjust(checktemplet,uemp);  //工资调整 （正式或者派遣离职-扣减）(需要放在最后跑批)
			adjuestCtrlImpl.insertLeavebmoney(checktemplet,uemp);   // 请假扣款
			adjuestCtrlImpl.insertOvertimemoney(checktemplet,uemp);  //加班工资
			adjuestCtrlImpl.insertOvertimemeals(checktemplet,uemp);  //加班餐补
			adjuestCtrlImpl.insertShiftallowance(checktemplet,uemp);  //轮班津贴
			adjuestCtrlImpl.insertLoyalward(checktemplet,uemp);      //忠诚奖
			//-----------------
			dutyseviceImp.savedutymoney(checktemplet,uemp); //税前工资没有完整的数据
			dutyseviceImp.savepersonalmoney(checktemplet,uemp);//个人保险金
			dutyseviceImp.saveDutydeduct(checktemplet,uemp);//税前扣除
			dutyseviceImp.savedutysendmoney(checktemplet,uemp);//税前应发工资
			dutyseviceImp.savetradecost(checktemplet,uemp);//工会费 
			dutyseviceImp.savedutyback(checktemplet,uemp);//税后饭贴
			dutyseviceImp.savepersonaldutyone(checktemplet,uemp);//个人所得税 
			dutyseviceImp.savedutycount(checktemplet,uemp);//税后应发合计
			dutyseviceImp.savedutym(checktemplet,uemp);//税前饭贴
			dutyseviceImp.saveminomey(checktemplet,uemp);//最低工资
			dutyseviceImp.savesolidmoney(checktemplet,uemp);//实发工资
			dutyseviceImp.savesecureunit(checktemplet,uemp);//单位保险金缺数据
			dutyseviceImp.savedispatchmanger(checktemplet,uemp);//派遣员工管理费			
		} catch (Exception e) {
			e.printStackTrace();
			logger.debug("-----计算执行错误"+System.currentTimeMillis());
			//continue ;
		}  //补发
		
		return "计算成功！";
	}
}
